package liquidfs;

import java.io.*;
import java.util.*;

import com.thoughtworks.xstream.annotations.*;

import liquidfs.core.*;
import liquidfs.data.*;

@XStreamAlias("configuration")
public class Configuration {

   @XStreamOmitField()
   private Application app;
   
   @XStreamOmitField()
   private String configurationPath = null;
   
	protected int messagePort = 4474;
	protected int dataPort = 4475;
	protected int webPort = 4476;
	protected int ftpPort = 4472;
	
	protected String mountPoint = "Q:";
	protected String backendPath = "C:/Scratch/LiquidFS";
	protected String volumeName = "LiquidVolume";
	
	protected PeerDevice localDevice = new PeerDevice ("TSURUGI", "Tsurugi");
	
	protected List<Link> links = new ArrayList<Link>();
	
	private boolean vfsEnabled = true;
   private boolean ftpEnabled = true;
   private boolean xmlCompression = true;
   private boolean webInterfaceEnabled = true;
	
	protected Configuration (Application app) {
	   this.app = app;
	}
	
	public static Configuration createDefaultConfiguration (Application app) {
	   return new Configuration(app);
	}
	
	public static Configuration loadConfiguration(Application app, String path) throws IOException {
	   Serializer serializer = Serializer.getPlainSerializer(app);
	   Configuration conf = (Configuration) serializer.deserialize(new FileInputStream(path));
	   conf.configurationPath = path;
	   
	   return conf;
	}
	
	public void save() throws IOException {
	   Serializer serializer = Serializer.getPlainSerializer(app);
      serializer.serialize(this, new FileOutputStream(this.configurationPath));
	}
	
	public void save(String path) throws IOException {
	   Serializer serializer = Serializer.getPlainSerializer(app);
	   serializer.serialize(this, new FileOutputStream(path));
	}
	
	public PeerDevice getLocalDevice () {
		return localDevice;
	}
	
	public Host getLocalHost () {
	   return new Host("localhost", this.messagePort);
	}
	
	public Link getLocalLink () {
	   return new Link(this.localDevice, this.getLocalHost());
	}
	
	public String getMountPoint() {
		return mountPoint;
	}
	
	public String getBackendPath () {
		return this.backendPath;
	}
	
	public String getBackendFiles () {
		return this.backendPath + "/data";
	}
	
	public String getJournalPath () {
	   return this.backendPath + "/journal/journal.xml";
	}
	
	public String getQueuePath () {
	   return this.backendPath + "/journal";
	}
	
	public String getVolumeName () {
	   return this.volumeName;
	}
	
	public int getMessagePort () {
		return this.messagePort;
	}
	
	public int getDataPort () {
		return this.dataPort;
	}
	
	public int getWebPort () {
		return this.webPort;
	}
	
	public int getFtpPort () {
	   return this.ftpPort;
	}
	
	public List<Link> getLinks () {
		return links;
	}
	
	public PeerDevice getPeerDevice (String id) {
	   for (Link link : this.getLinks()) {
	      if (link.getDevice().getID().equalsIgnoreCase(id)) {
	         return link.getDevice();
	      }
	   }
	   
	   return new PeerDevice(id, id);
	}
	
	public boolean isVirtualFileSystemEnabled () {
	   return vfsEnabled;
	}
	
	public void setVirtualFileSystemEnabled(boolean enabled)	{
	   vfsEnabled = enabled;
	}

   public boolean isFtpServerEnabled() {
      return ftpEnabled ;
   }
   
   public void setFtpServerEnabled (boolean enabled) {
      this.ftpEnabled = enabled;
   }
	
   public boolean isXmlCompressionEnabled () {
      return this.xmlCompression;
   }
   
   public void setXmlCompressionEnabled (boolean enabled) {
      this.xmlCompression = enabled;
   }
   
   public boolean isWebInterfaceEnabled () {
      return this.webInterfaceEnabled;
   }
   
   public void setWebInterfaceEnabled (boolean enabled) {
      this.webInterfaceEnabled = enabled;
   }
}
